package com.itheima.tlias08_14.service.impl;

import com.itheima.tlias08_14.dto.SelectDeptDTO;
import com.itheima.tlias08_14.dto.StatisticEmpGenderDTO;
import com.itheima.tlias08_14.entity.Department;
import com.itheima.tlias08_14.entity.DepartmentDTO;
import com.itheima.tlias08_14.mapper.DepartmentMapper;
import com.itheima.tlias08_14.service.DeptService;
import com.itheima.tlias08_14.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImp implements DeptService {


    //@Resource 与 @Autowired 都是Spring提供的注解，用于依赖注入。
    @Autowired
    //@Autowired 等同于 new DepartmentMapper()；
            DepartmentMapper mapper;

    @Override
    public ArrayList<SelectDeptDTO> selectAllDTO() {
        ArrayList<SelectDeptDTO> departments = mapper.selectAllDTO();
        return departments;
    }

    @Override
    public ResultUtil insertDept(Department dept) {
        //判断部门名称是否重复
        Department department = mapper.selectDeptByName(dept.getName());
        if (department != null && dept.getName().equals(department.getName())) {
            return ResultUtil.failure("部门名称重复");
        }
        int i = mapper.insertDept(dept);
        return ResultUtil.success();


    }

    @Override
    public ResultUtil deleteDeptById(Integer id) {
        //删除部门之前得判断该部门中是否还有员工存在
        //获取每个部门的人数和对应的部门名称
        List<StatisticEmpGenderDTO> depts = mapper.countDeptEmployee();
        //获取需要删除的部门对象
        SelectDeptDTO selectDeptDTO = mapper.selectDeptById(id);
        for (StatisticEmpGenderDTO dept : depts) {
            if (dept.getName() != null && dept.getName().equals(selectDeptDTO.getName())) {
                if (dept.getValue() > 0) {
                    return ResultUtil.failure("该部门中还有员工，无法删除");
                }
                break;
            }
        }
        int i = mapper.deleteDeptById(id);
        return ResultUtil.success();
    }

    @Override
    public SelectDeptDTO selectDeptById(Integer id) {
        SelectDeptDTO department = mapper.selectDeptById(id);
        return department;
    }


    @Override
    public ResultUtil updateDept(Department dept) {
        dept.setUpdateTime(LocalDateTime.now());
        //判断部门名称是否重复
        Department department = mapper.selectDeptByName(dept.getName());
        if (department != null && !dept.getId().equals(department.getId())
                && dept.getName().equals(department.getName())) {
            return ResultUtil.failure("部门名称重复");
        } else {
            mapper.updateDept(dept);
            return ResultUtil.success("更新成功" + dept);
        }
    }

    @Override
    public Department selectDeptByName() {
        return null;
    }

    @Override
    public int batchDelete(List<Integer> list) {
        int i = mapper.batchDelete(list);
        return i;
    }

    @Override
    public int batchInset(List<DepartmentDTO> list) {
        //插入前判断是否已经存在该部门
        int i = mapper.batchInset01(list);
        return i;
    }
}
